package com.taojie.graduation.controller;

import com.taojie.graduation.domain.User;
import com.taojie.graduation.service.api.UserService;
import com.taojie.graduation.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@CrossOrigin
@RequestMapping("user")
@Api(tags = {"用户接口"})
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation("分页查询")
    @GetMapping("{page}/{size}")
    public Result page(@PathVariable("page") int page, @PathVariable("size") int size, @RequestParam(value = "query", required = false) String query){
        return userService.findUserPage(page, size, query);
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("{id}")
    public Result findUserById(@PathVariable("id") Integer id){
        return userService.findUserById(id);
    }

    @ApiOperation("根据id更新用户")
    @PutMapping("{id}")
    public Result updateUserById(@PathVariable("id") Integer id,@RequestBody User user){
        user.setId(id);
        return userService.updateUserById(user);
    }

    @ApiOperation("根据id删除用户")
    @DeleteMapping("{id}")
    public Result deleteUserById(@PathVariable("id")Integer id){
        return userService.deleteUserById(id);
    }

    @ApiOperation("添加用户")
    @PostMapping
    public Result addUser(@RequestBody User user) {
        return userService.insertUser(user);
    }


}
